package logic

import (
	"context"
	"shop/pkg"
	"shop/shopRpc/shoprpcclient"
	"strconv"

	"shop/api/internal/svc"
	"shop/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RealNameLogic) RealName(req *types.RealNameRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	userIdStr, ok := l.ctx.Value("user_id").(string)
	if !ok {
		return &types.Response{
			Code:    401,
			Message: "未授权",
			Data:    nil,
		}, nil
	}
	if !pkg.Real(req.IDCard, req.RealName) {
		return &types.Response{
			Code:    500,
			Message: "实名认证接口验证失败",
			Data:    nil,
		}, nil
	}
	userId, _ := strconv.Atoi(userIdStr)

	userReal, err := l.svcCtx.ShopRpc.UserReal(l.ctx, &shoprpcclient.UserRealRequest{
		RealName: req.RealName,
		IdCard:   req.IDCard,
		UserId:   int64(userId),
	})
	if userReal.UserId == 0 {
		return &types.Response{
			Code:    500,
			Message: "实名认证失败",
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "实名认证成功",
		Data:    nil,
	}, nil
}
